package com.nut.course.service.impl;

import com.nut.course.api.req.TagUsersReq;
import com.nut.course.common.Result;
import com.nut.course.service.TagService;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.cp.api.WxCpService;
import me.chanjar.weixin.cp.api.WxCpTagService;
import me.chanjar.weixin.cp.bean.WxCpTag;
import me.chanjar.weixin.cp.bean.WxCpTagGetResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class TagServiceImpl implements TagService {

    @Autowired
    private WxCpService wxCpService;

    @Override
    public Result create(String name) {
        try {
            String s = wxCpService.getTagService().create(name, null);
            log.info("create_result=" + s);
        } catch (WxErrorException e) {
            e.printStackTrace();
            return Result.fail(e.getMessage());
        }
        return Result.ok();
    }

    @Override
    public Result update(String name, String id) {
        try {
            wxCpService.getTagService().update(id, name);
        } catch (WxErrorException e) {
            e.printStackTrace();
            return Result.fail(e.getMessage());
        }
        return Result.ok();
    }

    @Override
    public Result delete(String id) {
        try {
            wxCpService.getTagService().delete(id);
        } catch (WxErrorException e) {
            e.printStackTrace();
            return Result.fail(e.getMessage());
        }
        return Result.ok();
    }

    @Override
    public Result getById(String id) {
        WxCpTagGetResult result;
        try {
            result = wxCpService.getTagService().get(id);
            return Result.ok(result);
        } catch (WxErrorException e) {
            e.printStackTrace();
            return Result.fail(e.getMessage());
        }
    }

    @Override
    public Result addTagUsers(TagUsersReq tagUsersReq) {

        WxCpTagService tagService = wxCpService.getTagService();
        try {
            tagService.addUsers2Tag(tagUsersReq.getTagId(), tagUsersReq.getUserIds(), null);
        } catch (WxErrorException e) {
            e.printStackTrace();
            return Result.fail(e.getMessage());
        }
        return Result.ok();
    }

    @Override
    public Result delTagUsers(TagUsersReq tagUsersReq) {

        WxCpTagService tagService = wxCpService.getTagService();
        try {
            tagService.removeUsersFromTag(tagUsersReq.getTagId(), tagUsersReq.getUserIds(), null);
        } catch (WxErrorException e) {
            e.printStackTrace();
            return Result.fail(e.getMessage());
        }
        return Result.ok();
    }

    @Override
    public Result<List<WxCpTag>> list() {
        WxCpTagService tagService = wxCpService.getTagService();
        try {
            List<WxCpTag> wxCpTags = tagService.listAll();
            return Result.ok(wxCpTags);
        } catch (WxErrorException e) {
            e.printStackTrace();
            return Result.fail(e.getMessage());
        }
    }
}
